<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-08
 * Time: 14:50
 */

namespace app\api\controller;


use app\common\model\UserAction;
use app\common\model\UserStudent;
use app\model\UserModel;
use app\common\model\UserWechat;
use app\common\model\User;

use app\service\LoginService;
use app\service\LogService;
use app\service\MsgService;
use app\service\UserService;
use think\Db;

class Base extends AllBase
{


    public $uid;
    public $sid;
    public $member;
    public $student;
    public $userModel;

    public static $url_array = array(
        'shop1/pages/cart/index' => '主商城-购物车',
        'shop1/pages/index/index' => '主商城-商城首页',
        'shop1/pages/jiameng/index' => '主商城-加盟，购买总代等',
        'shop1/pages/me/index' => '主商城-个人中心',
        'shop1/pages/order/class_pay' => '主商城-购买课程，课程直接支付',
        "shop1/pages/goods_detail/index" => "商品详情",
        'shop1/pages/search/index' => '主商城-搜索商品',
        'shop2/pages/cart/index' => '积分商城-购物车',
        'shop2/pages/goods_detail/index' => '积分商城-商品详情',
        'shop2/pages/index/index' => '积分商城首页',
        'shop2/pages/order/index' => '订单列表',
        'shop3/pages/goods_detail/index' => '生态商城-商品详情',
        'shop3/pages/index/index' => '生态商城首页',
        'shop3/pages/order/index' => '生态商城-订单列表',
        'chat/pages/article_detail/index' => '文章详情',
        'chat/pages/class_detail/index' => '课程页面',
        'chat/pages/index/index' => '聊天入口',
        'chat/pages/sucai_detail/index' => '素材详情',
        'chat/pages/tuiguang/edit_card' => '素材卡片',
        'chat/pages/tuiguang/index' => '推广入口',
    );

    public function __construct()
    {
        parent::__construct();
        self::checkToken();
        //dump($this->member);die;
        $this->userModel = new UserModel();
        $this->member = UserService::S()->findUser(array('id' => $this->uid));
        $user_student = UserStudent::getOne(['uid'=>$this->uid,'sid'=>$this->sid]);
        $student =$user_student? \app\common\model\Student::getOne(['id' => $this->sid]):array();
        if ($student) {
            $student['images'] = json_decode($student['images'], true);
        }
        $this->student = $student;


        self::logaction();//记录用户登录
    }

    public function checkToken()
    {
        //LogService::S()->writeLog('service','st',$_SERVER);
        $session_token = input("session_token");
        //$session_token="44cec26f91c1e713d82a7ff2c63850f0";//input("session_token");
        //dump($session_token);die;
        if (empty($session_token)) {
            $this->ajax(203, '请重新登录');
        }
        $this->uid = LoginService::S()->checkTokenBackUid($session_token);
        //dump($this->uid);die;
        if (!$this->uid) {
            $this->ajax(203, 'token失效');
        }

        $this->sid = input('sid', 0);

        $this->member = UserService::S()->findUser(array('id' => $this->uid));
        $wechatUser = UserWechat::getOne([
            'uid' => $this->uid
        ]);
        if ($wechatUser['subscribe'] == 0) {
            $res = $this->officialAccount->user->get($wechatUser['openid']);
            if ($res['subscribe'] == 1) {
                UserWechat::where('uid', $this->uid)->update([
                    'subscribe' => 1
                ]);
            }
        }
        //dump($this->member);die;
    }

    //记录用户行为
    public function logaction()
    {
        $url = $_SERVER['HTTP_REFERER'];//"http://tjr.xixi05.cn/m/chat/pages/index/index";
        $refer_http = explode("m/", $url);
        if (empty($refer_http[1])) {
            $refer_http[1] = "shop1/pages/index/index";
        }
        $http = explode("?", $refer_http[1]);//隔断参数

        $send_data['first'] = "用户访问通知";
        $send_data['nickname'] = $this->member['nickname'];
        $send_data['ziyuan'] = config('shop_name');
        $send_data['remark'] = "用户访问通知";
        //查询是否存在该访问记录
        $log_where['uid'] = $this->uid;
        $log_where['action'] = $http[0];
        $actionList = UserAction::getOne($log_where, "id,num");

        if ($actionList) { //存在新增次数
            $savelog['num'] = $actionList['num'] + 1;
            $savelog['update_time'] = date("Y-m-d H:i:s");//date("")
            UserAction::update($savelog, array('id' => $actionList['id']));
        } else {
            $log_data['refer_id'] = $this->member['refer_id'];
            $log_data['uid'] = $this->uid;
            $log_data['action'] = $http[0];
            if (empty($log_data['action'])) {
                $http[0] = "shop1/pages/index/index";
                $log_data['action'] = $http[0];
            }

            $log_data['cname'] = self::$url_array[$http[0]];
            $log_data['create_time'] = date("Y-m-d H:i:s");
            $log_data['update_time'] = date("Y-m-d H:i:s");
            $log_data['num'] = 1;
            UserAction::create($log_data);
        }

        if (!empty(self::$url_array[$http[0]])) { //存在
            $send_data['project'] = self::$url_array[$http[0]];
            if (cache("{$this->uid}-{$http[0]}") == 1) {//存在cookie
                return false;
            } else {
                MsgService::S()->sendTemp(MsgService::$tempType_NEW_USER_FANGWEN, $this->mall_id, $this->member['refer_id'], $send_data, "");
            }

        } else {
            if (empty($http[0])) { //
                $send_data['project'] = "商城首页";//self::$array[$http[1]];
                //dump($data);die;
                if (cache("{$this->uid}-{$http[0]}") == 1) {//存在cookie
                    return false;
                } else {
                    MsgService::S()->sendTemp(MsgService::$tempType_NEW_USER_FANGWEN, $this->mall_id, $this->member['refer_id'], $send_data, "");
                }
            }

        }

        cache("{$this->uid}-{$http[0]}", 1, 1800);
        //setcookie("{$this->uid}-{$http[0]}",1,300);

        //查询改行为是否已记录
    }


}